/** *********************************************************************** */
/*                                                                        */
/* System Name : ICIM */
/*                                                                        */
/* Description : Bis_param Management */
/*                                                                        */
/* MODIFICATION HISTORY */
/* Date Ver Name Description */
/* ---------- ----- ----------- ----------------------------------------- */
/*                                                                        */
/* 2017/10/12 N0.00                    Initial release                     */
/** *********************************************************************** */

$(document).ready(function() {

    var YNBean= {
        Value: ['Y', 'N'],
        Text: {
            Y: 'Y:YES',
            N: 'N:NO'
        },
        mustSaveFlg : 'N'
    };
	$("form").submit(function() {
				return false;
			});
	/*-------------------------------选择器初始化设置-----------------------*/
	var DomFactory = {
		$mdlIDSel   : $("#mdlIDSel"),
		$pathIDSel  : $("#pathIDSel"),
		$pathVerSel : $("#pathVerSel"),
		$boxSetCodeSel  : $("#boxSetCodeSel"),
		$opeIDTxt   : $("#opeIDTxt"),
		$opeVerTxt  : $("#opeVerTxt"),
		$opeNOSel   : $("#opeNOSel"),
		$window     : $(window),
		$queryPamDialog : $("#queryPamDialog"),
		$pamItemDialog : $("#pamItemDialog"),

		Dialog : {
			$queryPamDialog_mdlIDTxt : $("#queryPamDialog_mdlIDTxt"),
			$queryPamDialog_pathIDTxt : $("#queryPamDialog_pathIDTxt"),
			$queryPamDialog_pathVerTxt : $("#queryPamDialog_pathVerTxt"),
			$queryPamDialog_opeIDTxt : $("#queryPamDialog_opeIDTxt"),
			$queryPamDialog_opeVerTxt : $("#queryPamDialog_opeVerTxt"),
			//卡匣设定代码
			$queryPamDialog_maskSetIdTxt : $("#queryPamDialog_maskSetIdTxt"),
			//设备代码
			$pamItemDialog_toolIDSel : $("#pamItemDialog_toolIDSel"),

			$pamItemDialog_recipeIDTxt :$("#pamItemDialog_recipeIDTxt"),

			$pamItemDialog_photomaskIDTxt:$("#pamItemDialog_photomaskIDTxt"),
			//激活标志
			$pamItemDialog_activeflgTag:$("#pamItemDialog_activeflgTag"),
			//跳站标识
			$pamItemDialog_skipflgTag:$("#pamItemDialog_skipflgTag"),
			//量测名称
			$pamItemDialog_mesIdTag:$("#pamItemDialog_mesIdTag"),
			//箱子设定代码
			$pamItemDialog_boxSetCodeTag:$("#pamItemDialog_boxSetCodeTag"),
			//判等操作标识
			$pamItemDialog_gdJudegFlgTag:$("#pamItemDialog_gdJudegFlgTag"),
			//是否限定重工次数
			$pamItemDialog_swhCntAddFlgTag:$("#pamItemDialog_swhCntAddFlgTag"),
			//swh cnt add pos
			$pamItemDialog_swhCntAddPosTag:$("#pamItemDialog_swhCntAddPosTag"),
			//最大重工次数
			$pamItemDialog_swhMaxCntTag:$("#pamItemDialog_swhMaxCntTag"),
		},

		Grid: {
		$pamListDiv : $("#pamListDiv"),
		$pamListGrd	:	$("#pamListGrd"),
		$pamListPg : $("#pamListPg"),

		$pamItemListDiv : $("#pamItemListDiv"),
		$pamItemListGrd :  $("#pamItemListGrd"),
		$pamItemListPg : $("#pamItemListPg")
		}
	};

	var routeBean = CreateRouteBean();
	var VAL = {
		//用到的
		T_FBPBISPAM : "FBPBISPAM",
		T_FBPBISPTH : "FBPBISPTH",
		T_FBPBISOPE : "FBPBISOPE",
		T_FBPMDLDEF : "FBPMDLDEF",
		T_FBPBISTOL	: "FBPBISTOL",

		NORMAL     : "0000000",
		YES        : "Y" ,
		NO         : "N" ,
		DISABLED_ATTR : {'disabled' : true },
		ENABLED_ATTR  : {'disabled' : false},
        CPFLAG : "N"
	};

	/*---------------------------------------------------------------------------*/
	var CheckBoxDom = {
		setCheckBox : function($domObj, checkFlg){
			if (checkFlg) {
				$domObj.attr("checked","true");
			} else {
				$domObj.removeAttr("checked");
			}
		},
		isChecked : function($domObj){
			return ($domObj.attr("checked") === "checked" ? true : false);
		}
	};

	//将值付给iary数组对象
	function comSetIary(iary, prop, val) {
		if (val) {
			iary[prop] = val;
		}
	}
	//根据表格得到
	function getAddRowID($grid) {
		//返回当前grid里所有数据的id
		var rowIds = $grid.jqGrid("getDataIDs");

		if(rowIds.length>0){
			return parseInt(rowIds[rowIds.length-1]) + 1;
		}else{
			return "1";
		}
	/*	//如果行数大于0，那么就返回最后一个行的行数，否则就返回为第一行。
		return rowIds.length > 0 ? (rowIds[rowIds.length - 1]) + 1 : "1";*/
	}
	/*------------------------------------初始化两个表格----------------------------*/
	var initGrdInfo = {
		initpamListGrd : function () {
			//Parameter信息
			DomFactory.Grid.$pamListGrd.jqGrid({
				url : "",
				datatype : "local",
				mtype : "POST",
				autowidth : true,
                shrinkToFit:false,
                scroll: true,
				resizable : true,
				loadonce : true,
				viewrecords : true,
				rownumbers : true,
				emptyrecords : true,
				pager: "pamListPg",
				colModel : [
				    {
				    	name : 'mdl_id_fk' ,				
				    	index : 'mdl_id_fk',				
				    	label : MDL_ID_TAG,				
				    	width : 140          
				    },{
				    	name : 'ope_no_fk',				
				    	index : 'ope_no_fk',				
				    	label : OPE_NO_TAG,				
				    	width : 130			
				    },{
				    	name : 'ope_id_fk',				
				    	index : 'ope_id_fk',				
				    	label : OPE_ID_TAG,				
				    	width : 130			
				    },{
				    	name : 'path_id_fk',				
				    	index : 'path_id_fk',				
				    	label : PATH_ID_TAG,		    
				    	width : 130			
				    }],
				//选中一行就进行加载详细信息
			    onSelectRow : function(rowId) {
			    	/*if (DomFactory.$boxSetCodeSel.attr("disabled") != "disabled") {
						showErrorDialog("", "目前处于编辑中，请勿进行查询操作");
						return false;
					}*/
			    	//先清空item的内容,再加载内容
			    	DomFactory.Grid.$pamItemListGrd.jqGrid("clearGridData");
			    	var outObj, mdl_id_fk,ope_no_fk,ope_id_fk,ope_ver_fk,path_id_fk,path_ver_fk,iary = {};
					//根据当前行的rowID获取当前行的数据
					var rowData = $("#pamListGrd").jqGrid("getRowData",rowId);

			    	mdl_id_fk=rowData.mdl_id_fk;
			    	tool_id_fk=rowData.tool_id_fk;
			    	path_id_fk=rowData.path_id_fk;
			    	path_ver_fk=rowData.path_ver_fk;
			    	ope_ver_fk=rowData.ope_ver_fk;
			    	ope_no_fk=rowData.ope_no_fk;
			    	ope_id_fk=rowData.ope_id_fk;

			    	iary.mdl_id_fk = mdl_id_fk;
			    	iary.tool_id_fk = tool_id_fk;
			    	iary.path_id_fk = path_id_fk;
			    	iary.path_ver_fk = path_ver_fk;
			    	iary.ope_ver_fk = ope_ver_fk;
			    	iary.ope_no_fk = ope_no_fk;
			    	iary.ope_id_fk = ope_id_fk;

			    	inObj_Query ={
			    			trx_id : VAL.T_FBPBISPAM,
							action_flg : 'E',
							iary : [iary]
			    	};
			    	outObj = comTrxSubSendPostJson(inObj_Query);
			    	var oary= outObj.oary[0];
					if (outObj.rtn_code === VAL.NORMAL) {
						//设置item表格信息
						setGridInfo(outObj.oary, "#pamItemListGrd", true);
						//如果有一行的设备ID为*，则隐藏这一行的数据(界面不显示这行数据，但在数据库里还是存在的)
						//暂时没有办法根除，因为新增必须要有toolId
						var rowIds = DomFactory.Grid.$pamItemListGrd.jqGrid("getDataIDs");
						for(var i=0;i<rowIds.length;i++){
							var rowData = DomFactory.Grid.$pamItemListGrd.jqGrid("getRowData",rowIds[i]);
							/*if(rowData.tool_id_fk === "*"){
								DomFactory.Grid.$pamItemListGrd.jqGrid('delRowData',rowIds[i]);
							}*/
						}
						SelectDom.setSelect(DomFactory.$mdlIDSel,oary.mdl_id_fk,oary.mdl_id_fk);
						SelectDom.setSelect(DomFactory.$pathIDSel,oary.path_id_fk,oary.path_id_fk);
						SelectDom.setSelect(DomFactory.$pathVerSel,oary.path_ver_fk,oary.path_ver_fk);
						SelectDom.setSelect(DomFactory.$boxSetCodeSel,oary.box_set_code,oary.box_set_code);
						SelectDom.setSelect(DomFactory.$opeNOSel,oary.ope_no_fk,oary.ope_no_fk);
						$("#opeIDTxt").val(oary.ope_id_fk);
						$("#opeVerTxt").val(oary.ope_ver_fk);
					}
				},
                beforeSelectRow :function (id) {
                    if(YNBean.mustSaveFlg == 'Y'){
                        showErrorDialog("","请先保存ITEM项！");
                        return false;
                    }
                    return true;
                }
			})
		},

		initpamItemListGrd : function() {
			DomFactory.Grid.$pamItemListGrd.jqGrid({
				url : "",
				datatype : "local",
				mtype : "POST",
				autowidth : true,
        		shrinkToFit:false,
				pager : "pamItemListPg",
				scroll : true,
				viewrecords : true,
				emptyrecords : true,
				toolbar : true,
				colModel: [
					{	
						name : 'tool_id_fk',			
						index : 'tool_id_fk',			
						label : TOOL_ID_TAG,		
						width : 280			
					},
					{	
						name : 'recipe_id',				
						index : 'recipe_id',			
						label : RECIPE_NO_TAG,
						width : 120			
					},{	
						name : 'active_flg',			
						index : 'active_flg',			
						label : ACTIVE_FLG_TAG,		
						width : 280			
					},{	
						name : 'skip_flg',				
						index : 'skip_flg',				
						label : SKIP_FLG_TAG,		
						width : 120,
						hidden:true
					},{	
						name : 'mes_id',				
						index : 'mes_id',				
						label : "上报参数代码",
						hidden:true,
						width : 280			
					},{	
						name : 'swh_cnt_add_flg',		
						index : 'swh_cnt_add_flg',		
						label : SWH_CNT_ADD_FLG_TAG,
                        hidden:true,
						width : 280			
					},{	
						name : 'swh_cnt_add_pos',		
						index : 'swh_cnt_add_pos',		
						label : SWH_CNT_ADD_POS_TAG,
						width : 120,
						hidden:true
					},{	
						name : 'swh_max_cnt',			
						index : 'swh_max_cnt',			
						label : SWH_MAX_CNT_TAG,
						width : 130	,
						hidden:true
					}]
			});
		}
	};
    
	//调用初始化函数
	initGrdInfo.initpamListGrd();
	initGrdInfo.initpamItemListGrd();
	
	//调整页面
	function resizeFnc(){
    	var offsetBottom, divWidth;

		offsetBottom = DomFactory.$window.height() - DomFactory.Grid.$pamListDiv.offset().top;
        DomFactory.Grid.$pamListGrd.setGridWidth(DomFactory.$window.width() - $('.rightDiv').width() - 120);
		DomFactory.Grid.$pamListGrd.setGridHeight(offsetBottom * 0.99 - 81);

		divWidth = DomFactory.$window.width() - DomFactory.Grid.$pamItemListDiv.offset().left;
        DomFactory.Grid.$pamItemListDiv.width(DomFactory.$window.width() - $('.leftDiv').width() - 120);
        $(".container").width(DomFactory.$window.width() - $('.leftDiv').width() - 120);
		DomFactory.Grid.$pamItemListGrd.setGridWidth(DomFactory.$window.width() - $('.leftDiv').width() - 120);
		offsetBottom = DomFactory.$window.height() - DomFactory.Grid.$pamItemListDiv.offset().top;
		DomFactory.Grid.$pamItemListGrd.setGridHeight(offsetBottom * 0.99 - 51);
    };
    
    DomFactory.$window.resize(function() {
    	resizeFnc();
	});
    
	//页面输入初始化函数
	function initFnc() {
		$("input").attr(VAL.DISABLED_ATTR);
		SelectDom.initWithSpace($("select"));
		$("select").attr(VAL.DISABLED_ATTR);
		$("#update_btn").attr(VAL.ENABLED_ATTR);
		$("input").val("");
		$("#pamItemListDiv").css({"margin-top" : $("#qrsListDiv").height() - 50});
	}

	/*-------------------------------初始化选择框函数-----------------------------*/
	var initSelectDom ={
		//4、	初始化选择框：卡匣设定代码!!
		initBoxSetCodeSel : function (){
			addValueByDataCateFnc("#boxSetCodeSel", "BXSC", "data_ext");
		}
	};
	
	/*---------------------------------查询对话模态框函数-----------------------------------*/
	var QueryFnc ={
		//首先初始化查询模态框
		/** * 将Div实例化为modal窗体 ** */
			diaLogQueryFnc : function(action_flg) {
			var iary = {},inObj,outObj;
			comSetIary(iary, "mdl_id_fk", $("#queryPamDialog_mdlIDTxt").val());
			comSetIary(iary, "path_id_fk", $("#queryPamDialog_pathIDTxt").val());
			comSetIary(iary, "path_ver_fk", $("#queryPamDialog_pathVerTxt").val());
			comSetIary(iary, "ope_id_fk", $("#queryPamDialog_opeIDTxt").val());
			comSetIary(iary, "ope_ver_fk", $("#queryPamDialog_opeVerTxt").val());
			comSetIary(iary, "tool_id_fk", $("#queryPamDialog_toolIDTxt").val());
			comSetIary(iary, "ope_no_fk", $("#queryPamDialog_opeNOTxt").val());

			inObjR = {
				trx_id : VAL.T_FBPBISPAM,
				action_flg : 'Q',
				iary : [iary],
				tbl_cnt : 1
			};
			outObj = comTrxSubSendPostJson(inObjR);
			if (outObj.rtn_code === VAL.NORMAL) {
				setGridInfo(outObj.oary, "#pamListGrd", true);
				$('#queryPamDialog').modal("hide");
			}
			//右边信息表清空
			initFnc();
			DomFactory.Grid.$pamItemListGrd.jqGrid("clearGridData");
		},

		//这个是按了添加后出现的新增框，或者修改框
		diaLogQueryFnc2 : function() {
			var  inObj, outObj, oary, tblCnt,newRowId,swhMaxCntTag;
			var actionFlg = DomFactory.Dialog.$pamItemDialog_toolIDSel.attr("disabled") === "disabled"
				? "U"
				: "A";
			var toolId=DomFactory.Dialog.$pamItemDialog_toolIDSel.val();
			if (!toolId) {
				showErrorDialog("", "设备代码不能为空");
				return false;
			}

			var recipeId=DomFactory.Dialog.$pamItemDialog_recipeIDTxt.val();
            if (!recipeId) {
                showErrorDialog("", "Recipe_NO不能为空");
                return false;
            }

			var active_flg = DomFactory.Dialog.$pamItemDialog_activeflgTag.val();
			if(!active_flg){
				showErrorDialog("", "激活标识不能为空");
				return false;
			}
			var skip_flg = DomFactory.Dialog.$pamItemDialog_skipflgTag.val();
		/*	if(!skip_flg){
				showErrorDialog("", "跳站标识不能为空");
				return false;
			}*/
			var mesId = DomFactory.Dialog.$pamItemDialog_mesIdTag.val();
			/*if(!mesId){
				showErrorDialog("", "量测名称不能为空");
				return false;
			}*/
			var swh_cnt_add_flg = DomFactory.Dialog.$pamItemDialog_swhCntAddFlgTag.val();
			if(!swh_cnt_add_flg){
				showErrorDialog("", "是否限定重工次数不能为空");
				return false;
			}

            if(actionFlg =="A"){
                //得到表格中所有的prdGrade，判重
                var rowDatas = DomFactory.Grid.$pamItemListGrd.getRowData();
                for(var i = 0 ;i < rowDatas.length; i++){
                    if(toolId == rowDatas[i].tool_id_fk){
                        showErrorDialog("", "设备代码已重复请重新输入！");
                        return false;
                    }
                }
                newRowId = getGridNewRowID(DomFactory.Grid.$pamItemListGrd);
                DomFactory.Grid.$pamItemListGrd.addRowData(newRowId,{'tool_id_fk':toolId,'recipe_id':recipeId,
					'active_flg':active_flg,'skip_flg':skip_flg,'mes_id':mesId,'swh_cnt_add_flg':swh_cnt_add_flg});
                //默认选择新加的行
                DomFactory.Grid.$pamItemListGrd.setSelection(newRowId, true);
                $('#pamItemDialog').modal("hide");
                showSuccessDialog("新增设备信息成功,点击保存确认！");
            }else if (actionFlg =="U"){
                //更新修改行的数据
                var selRowId = DomFactory.Grid.$pamItemListGrd.jqGrid("getGridParam","selrow");
                DomFactory.Grid.$pamItemListGrd.setRowData(selRowId,{'tool_id_fk':toolId,'active_flg':active_flg,'recipe_id':recipeId,'skip_flg':skip_flg,'mes_id':mesId,'swh_cnt_add_flg':swh_cnt_add_flg});
                $('#pamItemDialog').modal("hide");
                showSuccessDialog("修改设备信息成功,点击保存确认！");
            }
		}
	};

	//正则验证数字
	function f_check_naturalnumber(obj) {
		var s = obj;
		if (/^[0-9]+$/.test(s) && (s > 0)) {
			return true;
		} else {
			showErrorDialog("", "请输入正确的最大重工次数！");
			return false;
		}
	};

	/*---------------------------------------按钮函数------------------------*/
	var buttonFnc = {
		//1.查询
		queryFnc : function() {
            VAL.CPFLAG = 'N';
			$('#queryPamDialog').modal({
				backdrop : true,
				keyboard : false,
				show : false
			});

			$('#queryPamDialog').unbind('shown.bs.modal');
			$("#queryPamDialog_queryBtn").unbind('click');
			$('#queryPamDialog').bind('shown.bs.modal');
			$('#queryPamDialog').modal("show");
			//模态框查询按钮绑定查询函数
			$("#queryPamDialog_queryBtn").bind('click', QueryFnc.diaLogQueryFnc);
			$("#queryPamDialog input").attr(VAL.ENABLED_ATTR);
			$('#queryPamDialog input').val("");
			$("#update_btn").attr(VAL.ENABLED_ATTR);
		},

		//1.新增
		addFnc : function() {
            VAL.CPFLAG = 'N';
			//首先清空界面
			var inObj, outObj, i, tblCnt, oary;
			$("#pamConditionForm select").attr(VAL.ENABLED_ATTR);
			$("#opeIDTxt").attr(VAL.DISABLED_ATTR);
			$("#opeVerTxt").attr(VAL.DISABLED_ATTR);
			$("#pamConditionForm select").empty();
			$("#pamConditionForm input").empty();
			//修改按钮不可用
			$("#update_btn").attr(VAL.DISABLED_ATTR);
			//清空Items表格信息
			DomFactory.Grid.$pamItemListGrd.jqGrid("clearGridData");
			//新增初始化产品信息
			inObj = {
					trx_id : VAL.T_FBPMDLDEF,
					action_flg : "Q",
					iary : [{}]
				};

			outObj = comTrxSubSendPostJson(inObj);
			tblCnt = outObj.tbl_cnt;
			//初始化化*
			SelectDom.addSelect(DomFactory.$mdlIDSel,"*");
			SelectDom.addSelect(DomFactory.$pathIDSel,"*");
			SelectDom.addSelect(DomFactory.$pathVerSel,"*");
			SelectDom.addSelect(DomFactory.$opeNOSel,"*");
			$("#opeVerTxt").val("");
			$("#opeIDTxt").val("");

			var warp = document.createDocumentFragment();
            for (i = 0; i < tblCnt; i++) {
                var option = document.createElement("option");
                option.value = outObj.oary[i].mdl_id;
                option.text = outObj.oary[i].mdl_id;
                warp.appendChild(option);
            }
            DomFactory.$mdlIDSel.append(warp);

			initSelectDom.initBoxSetCodeSel();
			mdlIdSelFnc();
           /* initOpeSel();*/
		},
		//修改
		updateFnc: function() {
            VAL.CPFLAG = 'N';
			//先判断有没有选中行
			var selRowId = DomFactory.Grid.$pamListGrd.jqGrid("getGridParam","selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要修改的产品信息");
				return false;
			}
			//除了maskSetIdSel可以修改，其他主键不能修改
			$("#boxSetCodeSel").attr(VAL.ENABLED_ATTR);
			initSelectDom.initBoxSetCodeSel();

		},

		deleteFnc: function() {
            VAL.CPFLAG = 'N';
			var selRowId, rowData, iary, inObj, outObj;
			//获得选中的行ID
			selRowId = DomFactory.Grid.$pamListGrd.jqGrid("getGridParam",
					"selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要删除的Parameter!");
				return false;
			}
			//删除按钮绑定删除事件
			$("#del_btn").showCallBackWarnningDialog({
				errMsg : "是否删除该Parameter,请确认!!!!",
				callbackFn : function(data) {
					if (data.result == true) {
						//获得选中行的数据
						rowData = DomFactory.Grid.$pamListGrd.jqGrid(
								"getRowData", selRowId);
						iary = {
							mdl_id_fk: rowData.mdl_id_fk,
							tool_id_fk: rowData.tool_id_fk,
							ope_no_fk : rowData.ope_no_fk,
							ope_id_fk: rowData.ope_id_fk,
							ope_ver_fk: rowData.ope_ver_fk,
							path_id_fk: rowData.path_id_fk,
							path_ver_fk: rowData.path_ver_fk,
						};
						inObj = {
							trx_id : VAL.T_FBPBISPAM,
							action_flg : "D",
							iary : [iary]
						};
						outObj = comTrxSubSendPostJson(inObj);
						if (outObj.rtn_code === VAL.NORMAL) {
                            SelectDom.setSelect($("select"), "", "");
                            $("input").val("");

							showSuccessDialog("删除Parameter成功！");
							DomFactory.Grid.$pamListGrd.jqGrid(
									"delRowData", selRowId);
							//删除完后页面除了Parameter信息，清空
							DomFactory.Grid.$pamItemListGrd.jqGrid("clearGridData");
						}
					}
				}
			});

		},

		copyFnc: function() {
            VAL.CPFLAG = "Y";
            /*if (DomFactory.$boxSetCodeSel.attr("disabled") != "disabled") {
                showErrorDialog("", "目前处于编辑中，请勿进行查询操作");
                return false;
            }*/
            var selRowId = DomFactory.Grid.$pamListGrd.jqGrid('getGridParam','selrow');
            if(!selRowId){
                showErrorDialog("", "请选择需要复制的Parameter信息！");
                return false;
            }
            $("select").attr({'disabled':false});
            DomFactory.$mdlIDSel.focus();
            initSelectDom.initBoxSetCodeSel();
            mdlIdSelFnc();
		},

		rollbackFnc: function() {
            VAL.CPFLAG = 'N';
			if(DomFactory.$boxSetCodeSel.attr("disabled") == "disabled"){
				showErrorDialog("", "没有编辑，无需撤回");
				return false;
			}
			$("#pamConditionForm input").attr(VAL.DISABLED_ATTR);
			$("#pamConditionForm select").attr(VAL.DISABLED_ATTR);
			var outObj = comTrxSubSendPostJson(inObjR);
			if (outObj.rtn_code === VAL.NORMAL) {
				setGridInfo(outObj.oary, "#pamListGrd");
				$("#update_btn").attr("disabled",false);
				$("select").empty();
				/*$("#pamItemListGrd").trigger("reloadGrid")*/
				DomFactory.Grid.$pamItemListGrd.jqGrid("clearGridData");
			}
		},

		saveFnc : function(){
            VAL.CPFLAG = 'N';
            YNBean.mustSaveFlg = 'N';
			var actionFlg, inObj, outObj, iary, newRowId,rowIds;
			//可编辑说明为新增，不可编辑说明为修改
			actionFlg = DomFactory.$mdlIDSel.attr("disabled") === "disabled"
				? "U"
				: "A";
			//所有的select选项的数据
			var mdlID=DomFactory.$mdlIDSel.val();
			var pathID=DomFactory.$pathIDSel.val();
			var pathVer=DomFactory.$pathVerSel.val();
			var boxSetCode=DomFactory.$boxSetCodeSel.val();
			var opeNO=DomFactory.$opeNOSel.val();
			var opeID=$("#opeIDTxt").val();
			var opeVer=$("#opeVerTxt").val();

			//获得列表
            var rowIds = DomFactory.Grid.$pamItemListGrd.jqGrid("getDataIDs");
            if(rowIds.length == 0 ){
                showErrorDialog("","设备列表为空，请新增设备!");
                return false;
            }

			var iaryList = [];
            for(var i = 0; i < rowIds.length; i++){
                var rowData = DomFactory.Grid.$pamItemListGrd.jqGrid("getRowData",rowIds[i]);
                iary = {
                	tool_id_fk : rowData.tool_id_fk,
                	recipe_id : rowData.recipe_id,
					mask_set_id_fk : rowData.mask_set_id_fk,
					active_flg : rowData.active_flg,
					skip_flg : rowData.skip_flg,
					mes_id : rowData.mes_id,
					swh_cnt_add_flg :rowData.swh_cnt_add_flg,
					mdl_id_fk : mdlID,
                    path_id_fk : pathID,
                    path_ver_fk : '00000',
                    ope_id_fk : opeID,
                    ope_ver_fk : '00000',
                    box_set_code : boxSetCode,
                    ope_no_fk : opeNO
				}
                iaryList.push(iary);
			}
			//产品名称
			if(mdlID ==""){
				showErrorDialog("", "请先新增或修改!");
				return false;
			}

			//输给后台的数据
			inObj = {
					trx_id : VAL.T_FBPBISPAM,
					action_flg : actionFlg,
					iary : iaryList
				};
			$("select").attr(VAL.DISABLED_ATTR);

					outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code === VAL.NORMAL) {
                        if (actionFlg == "A") {
                            //加到表格的最后一行去，如果没有数据则加到第一行
                            newRowId = getAddRowID(DomFactory.Grid.$pamListGrd);
                            DomFactory.Grid.$pamListGrd.jqGrid("addRowData", newRowId,
                                outObj.oary);
                            //默认选择新加的行
                            DomFactory.Grid.$pamListGrd.setSelection(newRowId, true);
                            //保存成功后，修改按钮可用
                            $("#update_btn").attr(VAL.ENABLED_ATTR);
                            showSuccessDialog("新增Parameter信息成功");
                        } else if (actionFlg == "U") {
                            //返回正常
                                DomFactory.$boxSetCodeSel.val(outObj.oary[0].box_set_code);
                                showSuccessDialog("Parameter信息更新成功");
                            }
                        }
		},

		clearFnc : function(){
            VAL.CPFLAG = 'N';
			initFnc();
			//清空表格
			DomFactory.Grid.$pamListGrd.jqGrid("clearGridData");
			DomFactory.Grid.$pamItemListGrd.jqGrid("clearGridData");
		}
	};

	//按钮绑定事件
	$("#query_btn").bind('click',buttonFnc.queryFnc);
	$("#add_btn").bind('click',buttonFnc.addFnc);
	$("#update_btn").bind('click',buttonFnc.updateFnc);
	$("#del_btn").bind('click',buttonFnc.deleteFnc);
	$("#copy_btn").bind('click',buttonFnc.copyFnc);
	$("#save_btn").bind('click',buttonFnc.saveFnc);
	$("#rollback_btn").bind('click',buttonFnc.rollbackFnc);
	$("#clear_btn").bind('click',buttonFnc.clearFnc);

	/*-------------------------------Item按钮函数--------------------------------*/
	var itemButtonFnc = {
			//新增Item
		addItemFnc : function(){
            if (DomFactory.$boxSetCodeSel.attr("disabled") == "disabled") {
                showErrorDialog("", "目前不是编辑状态，不可新增设备信息。");
                return false;
            }
            YNBean.mustSaveFlg = 'Y';
			$('#pamItemDialog').modal({
				backdrop : true,
				keyboard : false,
				show : false
			});

			$('#pamItemDialog').unbind('shown.bs.modal');
			$("#pamItemDialog_addPamItemBtn").unbind('click');

			$('#pamItemDialog').bind('shown.bs.modal');
			$('#pamItemDialog').modal("show");
			//初始化化对话框
			$("#pamItemDialog input").attr(VAL.ENABLED_ATTR);
			$('#pamItemDialog input').val("");
			$('#pamItemDialog  select').attr(VAL.ENABLED_ATTR);
			$('#pamItemDialog  select').empty();
			//这里初始化查询框下拉框里的数据
			//1.inittoolID
			initItemSel.initToolId();
			//2.initswhcntaddflg
			initItemSel.initSwhCntAddFlg();
			//3.initswhcntaddpos
			/*initItemSel.initSwhCntAddPos();*/
			initItemSel.initActiveFlg();
			initItemSel.initSkipFlg();
			initItemSel.initMesId();
			initItemSel.initMaskSetId();
			//模态框查询按钮绑定查询函数 , 这里就是要执行查询函数了
			$("#pamItemDialog_addPamItemBtn").bind('click', QueryFnc.diaLogQueryFnc2);

		},
		delItemFnc: function(){
			var selRowId, rowData,inObj, outObj;
			var iary={};
			if (DomFactory.$boxSetCodeSel.attr("disabled") == "disabled") {
				showErrorDialog("", "目前不是编辑状态，不可删除设备信息。");
				return false;
			}
            YNBean.mustSaveFlg = "Y";
			//判断有没有选中pamListGrd表格行
			selRowId = DomFactory.Grid.$pamItemListGrd.jqGrid("getGridParam","selrow");
			if(selRowId == null){
				showErrorDialog("", "请先选择对应设备信息!");
				return false;
			}
			//删除按钮绑定删除事件
			$("#del_pamItem_btn").showCallBackWarnningDialog({
				errMsg : "是否删除该设备信息,请确认!!!!",
				callbackFn : function(data) {
					if (data.result == true) {
                        showSuccessDialog("删除该设备信息成功,请点击保存！");
                        DomFactory.Grid.$pamItemListGrd.jqGrid("delRowData", selRowId);
					}
				}
			});
		},

		updateItemFnc : function(){
            if (DomFactory.$boxSetCodeSel.attr("disabled") == "disabled") {
                showErrorDialog("", "目前不是编辑状态，不可更新设备信息。");
                return false;
            }
            YNBean.mustSaveFlg = "Y";
			//判断有没有选中pamListGrd表格行
			selRowId = DomFactory.Grid.$pamItemListGrd.jqGrid("getGridParam","selrow");
			if(selRowId == null){
				showErrorDialog("", "请先选择设备信息!");
				return false;
			}
			//初始化并弹出模态框
			$('#pamItemDialog').modal({
				backdrop : true,
				keyboard : false,
				show : false
			});

			$('#pamItemDialog').unbind('shown.bs.modal');
			$("#pamItemDialog_addPamItemBtn").unbind('click');

			$('#pamItemDialog').bind('shown.bs.modal');
			$('#pamItemDialog').modal("show");
			//初始化化对话框
			$("#pamItemDialog input").attr(VAL.ENABLED_ATTR);
			$('#pamItemDialog input').val("");
			$('#pamItemDialog  select').attr(VAL.ENABLED_ATTR);
			$('#pamItemDialog_toolIDSel').attr(VAL.DISABLED_ATTR);
			$('#pamItemDialog  select').empty();
			//这里初始化查询框下拉框里的数据
			//1.inittoolID(这里将表格中的tooId) 加载到模态框
			var selRowId = DomFactory.Grid.$pamItemListGrd.jqGrid("getGridParam","selrow");
			var rowData = DomFactory.Grid.$pamItemListGrd.jqGrid("getRowData",selRowId);
			SelectDom.addSelect(DomFactory.Dialog.$pamItemDialog_toolIDSel, rowData.tool_id_fk);
			//将表格中的数据加载到模态框中
			DomFactory.Dialog.$pamItemDialog_recipeIDTxt.val(rowData.recipe_id);
			DomFactory.Dialog.$pamItemDialog_swhMaxCntTag.val(rowData.swh_max_cnt);
			initItemSel.initSwhCntAddFlg();
			initItemSel.initActiveFlg();
			initItemSel.initSkipFlg();
			initItemSel.initMesId();
			initItemSel.initMaskSetId();
			//模态框查询按钮绑定查询函数 , 这里就是要执行查询函数了
			$("#pamItemDialog_addPamItemBtn").bind('click', QueryFnc.diaLogQueryFnc2);
		}
	};

	//ITem按钮绑定 (先要 选择pamater才能绑定)
	$("#add_pamItem_btn").bind("click",itemButtonFnc.addItemFnc);
	$("#upd_pamItem_btn").bind("click",itemButtonFnc.updateItemFnc);
	$("#del_pamItem_btn").bind("click",itemButtonFnc.delItemFnc);
	/*初始化item新增或修改下拉框的三个值*/
	var initItemSel ={
		initToolId : function (){

			var opeId,opeVer,iary,inObj,outObj,toolRunMode,iary2,inObj2,outObj2,tblCnt;
			opeId=$("#opeIDTxt").val();
			opeVer=$("#opeVerTxt").val();

			iary ={
					ope_id : opeId,
					ope_ver : opeVer
			};

			inObj = {
					trx_id : VAL.T_FBPBISOPE,
					action_flg : "Q",
					iary : [iary]
				};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				if(outObj.oary[0]!=null){
					toolRunMode=outObj.oary[0].tool_run_mode;
				}else{
					return;
				}
			}
			//下面是根据toolrunmode查找toolid
			iary2={
					tool_run_mode:toolRunMode
			};

			inObj2 = {
					trx_id : VAL.T_FBPBISTOL,
					action_flg : "H",
					iary : [iary2]
				};
			outObj2 = comTrxSubSendPostJson(inObj2);
			if (outObj2.rtn_code === VAL.NORMAL) {
				SelectDom.addSelectArr(DomFactory.Dialog.$pamItemDialog_toolIDSel, outObj2.oaryB, "tool_id", "tool_id","",true);
			}
		},

		initSwhCntAddFlg : function() {
			SelectDom.addSelect(DomFactory.Dialog.$pamItemDialog_swhCntAddFlgTag, "Y", "是");
			SelectDom.addSelect(DomFactory.Dialog.$pamItemDialog_swhCntAddFlgTag, "N", "否");
		},

		initActiveFlg : function (){
			SelectDom.addSelect(DomFactory.Dialog.$pamItemDialog_activeflgTag, "Y", "Y:YES");
			SelectDom.addSelect(DomFactory.Dialog.$pamItemDialog_activeflgTag, "N", "N:NO");
		},

		initSkipFlg : function () {
			SelectDom.addSelect(DomFactory.Dialog.$pamItemDialog_skipflgTag, "1", "自动入账");
			SelectDom.addSelect(DomFactory.Dialog.$pamItemDialog_skipflgTag, "2", "出账界面检查本站未入帐产品，协助发送入账请求");
			SelectDom.addSelect(DomFactory.Dialog.$pamItemDialog_skipflgTag, "3", "入账界面检查上一站点未出账，协助发送出账请求");
			SelectDom.addSelect(DomFactory.Dialog.$pamItemDialog_skipflgTag, "4", "跳过本站，不做制程");
		},

		//光罩代码
		initMaskSetId : function (){
			addValueByDataCateFnc("#pamItemDialog_photomaskIDTxt", "MASK", "data_item");
		},

		//量测名称
		initMesId : function () {
			addValueByDataCateFnc("#pamItemDialog_mesIdTag", "MLID", "data_ext");
		}
	};


	/*----------------------------select选择框 级联反应的函数----------------------------------*/
	//产品ID选择函数
	function mdlIdSelFnc() {
		var iary, inObj, outObj, i, oary, tblCnt, iaryB, mdlId;
		mdlId = DomFactory.$mdlIDSel.find("option:selected").html();
		iary = {
				mdl_id : mdlId
			};
		//产品ID不选择
		if (mdlId === "*") {
			inObj = {
				trx_id:VAL.T_FBPBISPTH,
				action_flg:"Q",
				iaryA : [{}]
			};
			//查询所有内容
			outObj  = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				DomFactory.$pathIDSel.empty();
				DomFactory.$pathVerSel.empty();
				//将工艺路线和工艺路线版本设置选项*
				SelectDom.addSelect(DomFactory.$pathIDSel, "*" );
				SelectDom.addSelect(DomFactory.$pathVerSel, "*");
				tblCnt = outObj.tbl_cnt_a;
				for (i = 0; i < tblCnt; i++) {
					SelectDom.addSelect(DomFactory.$pathIDSel, outObj.oaryA[i].path_id);
				}
			}
		} else {//选择产品ID
			inObj = {
				trx_id : VAL.T_FBPMDLDEF,
				action_flg : "Q",
				iary : [iary]
			};
			outObj  = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				DomFactory.$pathIDSel.empty();
				DomFactory.$pathVerSel.empty();
				SelectDom.addSelect(DomFactory.$pathIDSel, "*" );
				SelectDom.addSelect(DomFactory.$pathVerSel, "*");
				tblCnt = outObj.tbl_cnt;
				for (i = 0; i < tblCnt; i++) {
					SelectDom.addSelect(DomFactory.$pathIDSel, outObj.oary[i].path_id_fk);
				}
			}
		}
	};


	/*----------------------------工艺路线选择函数---------------------------------------*/
	function pathIdSelFnc(){
		var inObj, outObj, iaryB, oaryB, i, tblCnt,pathId,pathVer;

		pathId = DomFactory.$pathIDSel.val();
		pathVer = '00000';
		iaryB = {
				path_id_fk : pathId,
				path_ver_fk : pathVer
			};
			inObj = {
				trx_id : VAL.T_FBPBISPTH,
				action_flg : "I",
				iaryB : [iaryB]
			};
			$("#opeVerTxt").val("");
			$("#opeIDTxt").val("");
			DomFactory.$opeNOSel.empty();
			//SelectDom.initWithSpace(DomFactory.$opeNOSel);
        	SelectDom.addSelect(DomFactory.$opeNOSel,"*");
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				tblCnt = outObj.tbl_cnt_b;
				for (i = 0; i < tblCnt; i++) {
					//根据相应的工艺路线代码和工艺路线版本 得到ope_no
					SelectDom.addSelect(DomFactory.$opeNOSel, outObj.oaryB[i].cr_ope_no);
				}
			}
	};

    /*----------------------------------------ope_on选择函数----------------------------*/
	function opeNoSelFnc(){
		var inObj, outObj, iaryB, oaryB, i, tblCnt;
		var opeNo = DomFactory.$opeNOSel.val();
		iaryB = {
			path_id_fk : DomFactory.$pathIDSel.val(),
			// path_ver_fk : DomFactory.$pathVerSel.val(),
			path_ver_fk : '00000',
			cr_ope_no : opeNo
		};
		inObj = {
			trx_id : VAL.T_FBPBISPTH,
			action_flg : "I",
			iaryB : [iaryB]
		};
		$("#opeVerTxt").val("");
		$("#opeIDTxt").val("");

		outObj = comTrxSubSendPostJson(inObj);
		if (outObj.rtn_code === VAL.NORMAL) {
			tblCnt = outObj.tbl_cnt_b;
			for (i = 0; i < tblCnt; i++) {
					$("#opeVerTxt").val(outObj.oaryB[i].cr_ope_ver_fk);
					$("#opeIDTxt").val(outObj.oaryB[i].cr_ope_id_fk);
			}
		}
	};

	//绑定change函数
	DomFactory.$mdlIDSel.change(mdlIdSelFnc);
	// DomFactory.$pathIDSel.change(pathVerSelFnc);
	DomFactory.$pathIDSel.change(pathIdSelFnc);
	/*DomFactory.$pathVerSel.change(pathIdSelFnc);*/
	DomFactory.$opeNOSel.change(opeNoSelFnc);
    resizeFnc();
	initFnc();
    //enter
    /*document.onkeydown=function(event){
	 var e = event || window.event || arguments.callee.caller.arguments[0];
	 if(e && e.keyCode==13){ // 按 enter
	 $('.bootbox').modal("hide");
	 }
	 }*/
})
